/**
 * 
 */
package org.xlab.baseframework.service.impl;

import java.io.File;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.xlab.baseframework.service.api.ISystemMailService;
import org.xlab.baseframework.system.Declare;

/**
 * @author makubex
 * 
 */
@Service
public class SystemMailServiceImpl implements ISystemMailService {

	@Autowired(required = true)
	@Qualifier("bpf_mailSender")
	private JavaMailSender mailSender;
	@Autowired(required = true)
	@Qualifier("bpf_velocityEngine")
	private VelocityEngine velocityEngine;

	/**
	 * @return the mailSender
	 */
	public JavaMailSender getMailSender() {
		return mailSender;
	}

	/**
	 * @return the velocityEngine
	 */
	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	/**
	 * @see org.xlab.baseframework.service.api.ISystemMailService#sendMail(java.util.Map,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String[])
	 */
	public void sendMail(final Map<String, Object> model, final String subject,
			final String vmfile, final String to, final String cc,
			final String bcc, final String[] files) {
		String text = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, vmfile, Declare.SYSTEM_DEFAULT_CODE_SET, model);
		this.sendMail(subject, text, to, cc, bcc, files);
	}

	/**
	 * @see org.xlab.baseframework.service.api.ISystemMailService#sendMail(java.util.Map,
	 *      java.lang.String, java.lang.String, java.lang.String[],
	 *      java.lang.String[], java.lang.String[], java.lang.String[])
	 */
	public void sendMail(final Map<String, Object> model, final String subject,
			final String vmfile, final String[] to, final String[] cc,
			final String[] bcc, final String[] files) {
		String text = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, vmfile, Declare.SYSTEM_DEFAULT_CODE_SET, model);
		this.sendMail(subject, text, to, cc, bcc, files);
	}

	/**
	 * @see org.xlab.baseframework.service.api.ISystemMailService#sendMail(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String[])
	 */
	public void sendMail(final String subject, final String content,
			final String to, final String cc, final String bcc,
			final String[] files) {
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
						true, Declare.SYSTEM_DEFAULT_CODE_SET);
				message.setTo(to);
				if (cc != null && !cc.trim().isEmpty())
					message.setCc(cc);
				if (bcc != null && !bcc.trim().isEmpty())
					message.setBcc(bcc);
				if (subject != null && !subject.trim().isEmpty())
					message.setSubject(subject);
				if (content != null && !content.trim().isEmpty())
					message.setText(content, true);
				FileSystemResource file;
				if (files != null && files.length > 0) {
					for (String s : files) {
						file = new FileSystemResource(new File(s));
						message.addAttachment(s, file);
					}
				}
			}
		};
		mailSender.send(preparator);
	}

	/**
	 * @see org.xlab.baseframework.service.api.ISystemMailService#sendMail(java.lang.String,
	 *      java.lang.String, java.lang.String[], java.lang.String[],
	 *      java.lang.String[], java.lang.String[])
	 */
	public void sendMail(final String subject, final String content,
			final String[] to, final String[] cc, final String[] bcc,
			final String[] files) {
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
						true, Declare.SYSTEM_DEFAULT_CODE_SET);
				message.setTo(to);
				if (cc != null && cc.length > 0)
					message.setCc(cc);
				if (bcc != null && bcc.length > 0)
					message.setBcc(bcc);
				if (subject != null && !subject.trim().isEmpty())
					message.setSubject(subject);
				if (content != null && !content.trim().isEmpty())
					message.setText(content, true);
				FileSystemResource file;
				if (files != null && files.length > 0) {
					for (String s : files) {
						file = new FileSystemResource(new File(s));
						message.addAttachment(s, file);
					}
				}
			}
		};
		mailSender.send(preparator);
	}

	/**
	 * @param mailSender
	 *            the mailSender to set
	 */
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * @param velocityEngine
	 *            the velocityEngine to set
	 */
	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
